package com.easychat.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.entity.dto.TokenUserInfoDto;
import com.easychat.entity.dto.UserContactSearchResultDTO;
import com.easychat.entity.enums.PageSize;
import com.easychat.entity.enums.ResponseCodeEnum;
import com.easychat.entity.enums.UserContactStatusEnum;
import com.easychat.entity.enums.UserContactTypeEnum;
import com.easychat.entity.po.GroupInfo;
import com.easychat.entity.po.UserContactApply;
import com.easychat.entity.po.UserInfo;
import com.easychat.entity.query.UserContactApplyQuery;
import com.easychat.entity.query.UserContactQuery;
import com.easychat.entity.po.UserContact;
import com.easychat.entity.vo.PaginationResultVO;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.exception.BusinessException;
import com.easychat.service.GroupInfoService;
import com.easychat.service.UserContactApplyService;
import com.easychat.service.UserContactService;
import com.easychat.service.UserInfoService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 *  Controller
 */
@RestController("userContactController")
@RequestMapping("/contact")
public class UserContactController extends ABaseController{

	@Resource
	private UserContactService userContactService;

	@Resource
	private UserInfoService userInfoService;

	@Resource
	private GroupInfoService groupInfoService;

	@Resource
	private UserContactApplyService userContactApplyService;


	@GlobalInterceptor
	@RequestMapping("/search")
	public ResponseVO search(HttpServletRequest request, @NotEmpty String contactId) {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		UserContactSearchResultDTO resultDTO = userContactService.searchContact(tokenUserInfo.getUserId(), contactId);
		return getSuccessResponseVO(resultDTO);
	}

	@GlobalInterceptor
	@RequestMapping("/applyAdd")
	public ResponseVO applyAdd(HttpServletRequest request, @NotEmpty String contactId,
							   String applyInfo) {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		Integer joinType = userContactService.applyAdd(tokenUserInfo, contactId, applyInfo);
		return getSuccessResponseVO(joinType);
	}

	@GlobalInterceptor
	@RequestMapping("/loadApply")
	public ResponseVO loadApply(HttpServletRequest request, Integer pageNo) {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);

		PageInfo<UserContactApply> resultVO = userContactApplyService.selectApplyPage(pageNo, PageSize.SIZE15.getSize(), tokenUserInfo.getUserId());

		return getSuccessResponseVO(resultVO);
	}


	@GlobalInterceptor
	@RequestMapping("/dealWithApply")
	public ResponseVO dealWithApply(HttpServletRequest request, @NotNull Integer applyId, @NotNull Integer status) {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);

		userContactApplyService.dealWithApply(tokenUserInfo.getUserId(), applyId, status);

		return getSuccessResponseVO(null);
	}

	@GlobalInterceptor
	@RequestMapping("/loadContact")
	public ResponseVO loadContact(HttpServletRequest request, @NotNull String contactType) {
		UserContactTypeEnum contactTypeEnum = UserContactTypeEnum.getByName(contactType);
		if (null == contactTypeEnum) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		List<UserContact> userContactList = new ArrayList<>();
		if (Objects.equals(contactType, contactTypeEnum.USER.getAlias())) {
			userContactList = userContactService.selectUserContactList(tokenUserInfo.getUserId(), contactTypeEnum.getType());
		} else if (Objects.equals(contactType, contactTypeEnum.GROUP.getAlias())) {
			userContactList = userContactService.selectGroupContactList(tokenUserInfo.getUserId(), contactTypeEnum.getType());
		}

		return getSuccessResponseVO(userContactList);
	}

	@GlobalInterceptor
	@RequestMapping("/getContactInfo")
	public ResponseVO getContactInfo(HttpServletRequest request, @NotNull String contactId) {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		UserContactTypeEnum typeEnum = UserContactTypeEnum.getByPrefix(contactId);
		if (null == typeEnum) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		if (typeEnum.equals(UserContactTypeEnum.USER)) {
			UserInfo userInfoByUserId = userInfoService.getUserInfoByUserId(contactId);
			return getSuccessResponseVO(userInfoByUserId);
		} else if (typeEnum.equals(UserContactTypeEnum.GROUP)) {
			GroupInfo groupInfoByGroupId = groupInfoService.getGroupInfoByGroupId(contactId);
			return getSuccessResponseVO(groupInfoByGroupId);
		}
		return getSuccessResponseVO(null);
	}

	@GlobalInterceptor
	@RequestMapping("/delContact")
	public ResponseVO delContact(HttpServletRequest request, @NotNull String contactId) {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		userContactService.removeUserContact(tokenUserInfo.getUserId(), contactId, UserContactStatusEnum.DEL);
		return getSuccessResponseVO(null);
	}

	@GlobalInterceptor
	@RequestMapping("/addContact2BlackList")
	public ResponseVO addContact2BlackList(HttpServletRequest request, @NotNull String contactId) {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		userContactService.removeUserContact(tokenUserInfo.getUserId(), contactId, UserContactStatusEnum.BLACKLIST);
		return getSuccessResponseVO(null);
	}

	/**
	 * 获取用户信息,必须是好友
	 * @param request
	 * @param contactId
	 * @return
	 */
	@GlobalInterceptor
	@RequestMapping("/getUserInfo")
	public ResponseVO getUserInfo(HttpServletRequest request, @NotNull String contactId) {
		TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
		// 不是你的联系人就无法获取用户信息
		UserContact userContact = userContactService.getUserContactByUserIdAndContactId(tokenUserInfo.getUserId(), contactId);
		if (null == userContact || !ArrayUtils.contains(new Integer[]{
						UserContactStatusEnum.FRIEND.getStatus(),
						UserContactStatusEnum.DEL_BE.getStatus(),
						UserContactStatusEnum.BLACKLIST_BE.getStatus()
				},
				userContact.getStatus())) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		UserContactTypeEnum typeEnum = UserContactTypeEnum.getByPrefix(contactId);
		if (null == typeEnum) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		if (typeEnum.equals(UserContactTypeEnum.USER)) {
			UserInfo userInfoByUserId = userInfoService.getUserInfoByUserId(contactId);
			return getSuccessResponseVO(userInfoByUserId);
		} else if (typeEnum.equals(UserContactTypeEnum.GROUP)) {
			GroupInfo groupInfoByGroupId = groupInfoService.getGroupInfoByGroupId(contactId);
			return getSuccessResponseVO(groupInfoByGroupId);
		}
		return getSuccessResponseVO(null);
	}

}